package com.sky.backend.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.sky.backend.config.CommonConstants;
import com.sky.backend.exception.ExceptionEnum;
import com.sky.backend.pojo.Result;
import com.sky.backend.system.entity.SystemUser;
import com.sky.backend.system.service.SystemUserService;
import com.sky.backend.utils.MD5Utils;
import com.sky.backend.utils.ResultUtils;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/user")
public class SystemUserController {
    @Autowired
    private SystemUserService systemUserService;


    @RequestMapping("/logout")
    public String logout() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 20000);
        jsonObject.put("data", "success");

        return jsonObject.toJSONString();
    }

    @RequestMapping("/login")
    public Result login(@RequestBody SystemUser user) {
        user.setPassword(MD5Utils.stringToMD5(user.getPassword()));
        SystemUser findUser = systemUserService.selectByUsernamePassword(user);
        if (findUser == null) {
            ExceptionEnum.NOT_FOUND_USER.throwBussinessException("用户名或密码错误！");
        }


        Date date = new Date();
        String token = Jwts.builder().setSubject(findUser.getUsername())
                .claim("user", findUser)
                .setIssuedAt(date)
                .setExpiration(DateUtils.addSeconds(date, 30))
                .signWith(SignatureAlgorithm.HS256, CommonConstants.TOKEN_KEY)
                .compact();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("token", token);
        return ResultUtils.result(jsonObject1);

    }

//    {
//        roles: ['admin'],
//        introduction: 'I am a super administrator',
//                avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
//            name: 'Super Admin'
//    }

    @RequestMapping("/info")
    public String userInfo(@RequestParam String token) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 20000);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("roles", new String[]{"admin"});
        jsonObject1.put("introduction", "I am a super administrator");
        jsonObject1.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        jsonObject1.put("name", "超级管理员");
        jsonObject1.put("menues", new String[]{"测试菜单1", "测试菜单2"});

        jsonObject.put("data", jsonObject1);

        return jsonObject.toJSONString();
    }

    @RequestMapping("/insert")
    public int insert(SystemUser entity) {
        return systemUserService.insert(entity);
    }

    @RequestMapping("/deleteByPrimaryKey")
    public int deleteByPrimaryKey(Long id) {
        return systemUserService.deleteByPrimaryKey(id);
    }

    @RequestMapping("/updateByPrimaryKey")
    public int updateByPrimaryKey(SystemUser record) {
        return systemUserService.updateByPrimaryKey(record);
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<SystemUser> selectAll() {
        return systemUserService.selectAll();
    }
}